interface IBadges {
	index: number;
	type: string;
	text?: string;
}

/**
 * @see [description](https://docs.alipay.com/mini/api/ui-feedback)
 * show_action_sheet 显示操作菜单
 * @example show_action_sheet(['菜单一', '菜单二', '菜单三'],'标题','取消好了','0',[{ index: 0, type: 'none' },{ index: 1, type: 'point' },{index: 2, type: 'num', text: '99' }]);
 */
export default function show_action_sheet(items: string[], title?: string, cancelButtonText?: string, destructiveBtnIndex?: string, badges?: IBadges[]) {
	const options = {} as {
		title: string;
		cancelButtonText: string;
		destructiveBtnIndex: number;
		badges: IBadges[];
	};
	if (title !== undefined) {
		Object.assign(options, {
			title
		});
	}
	if (cancelButtonText !== undefined) {
		Object.assign(options, {
			cancelButtonText
		});
	}
	if (destructiveBtnIndex !== undefined) {
		Object.assign(options, {
			destructiveBtnIndex
		});
	}
	if (badges !== undefined) {
		Object.assign(options, {
			badges
		});
	}

	return new Promise<{
		msg: number;
		success: boolean
	}>((resolve, _reject) => {
		my.showActionSheet({
			...options,
			items,
			success(res: any) {
				resolve({
					msg: res.index,		// index===-1为取消
					success: true
				});
			},
			fail(err: any) {
				resolve({
					msg: err,
					success: false
				});
			}
		});
	});
}
